import { View, Button, Image } from '@tarojs/components'
import './index.scss'
import '../../../style/button.scss'
import IconFont from '../../iconfont/index.weapp'
import Taro from '@tarojs/taro';
import { useEffect, useState } from 'react';
import { user } from '../../../configProvider/backendInterface';
import { IP_NAME } from '../../../configProvider/namespace';

const Home: React.FC = () => {
  const [userInfo, setUserInfo] = useState<user>();
  useEffect(() => {
    getUserInfo()
  }, [])
  const getUserInfo = () => {
    Taro.getStorage({
      key: 'token',
      success: (res) => {
        Taro.request({
          method: 'POST',
          // url: `http://www.sinsen.cc:8081/wx/info?token=${res.data}`, //仅为示例，并非真实的接口地址
          url: `${IP_NAME}/wx/info?token=${res.data}`, //仅为示例，并非真实的接口地址
          data: {},
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            setUserInfo(res.data.data)
            if (!res.data.data.name || !res.data.data.phone) {
              Taro.reLaunch({ url: '../editInfo/index' })
            }
            Taro.setStorage({
              key: 'myInfo',
              data: res.data.data
            })
          }
        })
      }
    })
  }
  const editInfo = () => {
    Taro.navigateTo({
      url: '../editInfo/index'
    })
  }

  const startConsult = () => {
    Taro.navigateTo({
      url: '../informedConsent/index'
    })
  }
  const logOut = () => {
    Taro.clearStorage();
    Taro.reLaunch({ url: '../login/index' });
  }

  return (
    <View className='home'>

      <View className='user-info'>
        <View className='user-info-left'>
          <Image className='profile' src='https://pic2.imgdb.cn/item/64494af00d2dde57777da267.png'></Image>
        </View>
        <View className='user-info-right'>
          <View className='user-info-edit-icon' onClick={editInfo}>
            <IconFont name='edit' size={40} color='#999'></IconFont>
          </View>
          <View className='user-info-name'>{userInfo?.name}</View>
          <View className='user-info-phone'>{userInfo?.phone}</View>
        </View>
      </View>

      <Button onClick={startConsult} className='common-button'>立刻在线咨询</Button>

      <View className='consult-list'></View>

      <Button onClick={logOut} className='common-button'>退出登陆</Button>

    </View>
  )

};

export default Home;
